#include "stdafx.h"

#include "ToolBoxFloatDlg.h"
#include "..\Common\pkmpmsgbox.h"
#include "..\Common\Preferences.h"


LRESULT CToolBoxFloatDlg::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{		

	bAutoHide = thePrefs.m_TbxSettings.Auto_Hide ;
	bMakeTransparent = thePrefs.m_TbxSettings.Floating_Trpa;
	bChangeStyle = false;
	PrepareAutoHide(bAutoHide);
	bMouseInWindow = false;
	nOpacity = thePrefs.m_TbxSettings.FTransparent_Value;
	SetTimer(1, 1000, NULL);	// for show info 
	return 0;
}
BOOL CToolBoxFloatDlg::OnInitDialog(HWND wParam, LPARAM lParam)
{
	return TRUE;
}	
void CToolBoxFloatDlg::OnTimer(UINT timerID)
{
	if(bChangeStyle)
	{
		bChangeStyle = false;
		ModifyStyleEx(WS_EX_TOPMOST, 0, 0);
		::SetWindowPos(m_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
	}
	
	if(bMakeTransparent)
	{
		bMakeTransparent = false;
		ModifyStyleEx(0, WS_EX_LAYERED, 0);
		::SetLayeredWindowAttributes(m_hWnd, 0, nOpacity, LWA_ALPHA);
	}

	if(bStartHide)
	{
		bStartHide = false;
		WhenGoHide();
	}

	// show infomation
	
	SetMsgHandled(FALSE);
}
void CToolBoxFloatDlg::OnMove(CPoint pt)
{
	if(bAutoHide)
	{
		if(ehsHideState == hsAlwaysShow || ehsHideState == hsShowed)
			KeepInScreen(pt);
	}
	else
		KeepInScreen(pt);
	SetMsgHandled(FALSE);
}
void CToolBoxFloatDlg::KeepInScreen(CPoint pt)
{
	CRect r;
	this->GetWindowRect(&r);
	int nsw = ::GetSystemMetrics(SM_CXSCREEN);
	int nsh = ::GetSystemMetrics(SM_CYSCREEN);
	bool b = false;
	if(pt.x < 0)
	{
		r.left -= pt.x;
		r.right -= pt.x;
		b = true;
	}
	if(pt.y < 0)
	{
		r.top -= pt.y;
		r.bottom -= pt.y;
		b = true;
	}
	if(pt.x + r.Width() > nsw)
	{
		int dx = pt.x + r.Width() - nsw;
		r.left -= dx;
		r.right -= dx;
		b = true;
	}
	if(pt.y + r.Height() > nsh)
	{
		int dx = pt.y + r.Height() - nsh;
		r.top -= dx;
		r.bottom -= dx;
		b = true;
	}
	if(b)
		this->MoveWindow(&r);
}
void CToolBoxFloatDlg::WhenGoHide(void)
{
	//ShowWindow(SW_HIDE);
	::SendMessage(GetParent(),WM_COMMAND,(WPARAM)(ID_FLOATWIN_HIDE),(LPARAM)SW_HIDE);
	bHide = true;
	//InstallIcon(_T("SysInfo"), iconTray, IDR_MENURCONTEXT);
	//SetDefaultItem(ID_RCONTEXT_SHOW);
}

void CToolBoxFloatDlg::WhenGoShow(void)
{
	//ShowWindow(SW_SHOW);
	::SendMessage(GetParent(),WM_COMMAND,(WPARAM)(ID_TRAY_OPENTOOLBOX),(LPARAM)SW_SHOW);
	bHide = false;
	//RemoveIcon();
}

LRESULT CToolBoxFloatDlg::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	KillTimer(1);
	return 0;
}
//void CToolBoxFloatDlg::OnBkBtnClose()
//{
//
//	//ShowWindow(SW_HIDE);
//}

void CToolBoxFloatDlg::_init()
{
	
}	


void CToolBoxFloatDlg::OnMouseMove(UINT ui, CPoint pt)
{
	
	if(!bMouseInWindow)
	{
		bMouseInWindow = true;
		tmeTrack.cbSize = sizeof(tmeTrack);
		tmeTrack.dwFlags = TME_HOVER | TME_LEAVE;
		tmeTrack.dwHoverTime = HOVER_DEFAULT;
		tmeTrack.hwndTrack = m_hWnd;
		::TrackMouseEvent(&tmeTrack);
	}

}

void CToolBoxFloatDlg::OnMouseLeave(void)
{
	bMouseInWindow = false;
	tmeTrack.cbSize = sizeof(tmeTrack);
	tmeTrack.dwFlags = TME_CANCEL | TME_HOVER | TME_LEAVE;
	tmeTrack.dwHoverTime = HOVER_DEFAULT;
	tmeTrack.hwndTrack = m_hWnd;
	::TrackMouseEvent(&tmeTrack);

}

void CToolBoxFloatDlg::OnMouseHover(WPARAM wParam, CPoint pt)
{
	tmeTrack.cbSize = sizeof(tmeTrack);
	tmeTrack.dwFlags = TME_CANCEL | TME_HOVER | TME_LEAVE;
	tmeTrack.dwHoverTime = HOVER_DEFAULT;
	tmeTrack.hwndTrack = m_hWnd;
	::TrackMouseEvent(&tmeTrack);

	tmeTrack.cbSize = sizeof(tmeTrack);
	tmeTrack.dwFlags = TME_HOVER | TME_LEAVE;
	tmeTrack.dwHoverTime = HOVER_DEFAULT;
	tmeTrack.hwndTrack = m_hWnd;
	::TrackMouseEvent(&tmeTrack);

	
	
	if(bAutoHide)	// if auto hide, only in always show or showed states, show the popup
	{
		if(ehsHideState == hsAlwaysShow || ehsHideState == hsShowed)
		{
			//wndHistoryPopup.ShowPopup(m_hWnd, spi.pSetment);
		}
	}

}

void CToolBoxFloatDlg::OnLButtonDown(UINT ui, CPoint pt)
{
	//if(bShowHistoryPopup && smMaker.IsEnableHistory())
	//{
	//	tmeTrack.cbSize = sizeof(tmeTrack);
	//	tmeTrack.dwFlags = TME_CANCEL | TME_HOVER | TME_LEAVE;
	//	tmeTrack.dwHoverTime = HOVER_DEFAULT;
	//	tmeTrack.hwndTrack = m_hWnd;
	//	::TrackMouseEvent(&tmeTrack);
	//	//wndHistoryPopup.HidePopup(true);
	//	bMouseInWindow = false;
	//}
	this->SendMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(pt.x, pt.y));
}
void CToolBoxFloatDlg::OnLButtonDbClk(UINT ui, CPoint pt)
{
	if(bHide)
		WhenGoShow();
	else
		WhenGoHide();
}

void CToolBoxFloatDlg::OnRButtonUp(UINT ui, CPoint pt)
{
	/*if(bShowHistoryPopup && smMaker.IsEnableHistory())
	{
		tmeTrack.cbSize = sizeof(tmeTrack);
		tmeTrack.dwFlags = TME_CANCEL | TME_HOVER | TME_LEAVE;
		tmeTrack.dwHoverTime = HOVER_DEFAULT;
		tmeTrack.hwndTrack = m_hWnd;
		::TrackMouseEvent(&tmeTrack);
		wndHistoryPopup.HidePopup(true);
		bMouseInWindow = false;
	}*/

	CMenu rmenu;
	if(rmenu.LoadMenu(IDR_TRAYMENU))
	{
		this->ClientToScreen(&pt);
		//BeforeShowContextMenu(rmenu);
		rmenu.GetSubMenu(0).TrackPopupMenu(TPM_LEFTALIGN, pt.x, pt.y, this->m_hWnd);
		rmenu.DestroyMenu();
	}
}